using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using DARE;

namespace ModelWP7
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        #region fields

        private DARE.CDare m_Dare = null;
        private DARE.CScene m_Scene = null;
        private DARE.CRender3D m_Render = null;

        private DARE.CFirstPersonCamera m_Cam;

        #endregion

        #region ctor

        public Game1()
        {
            m_Dare = new DARE.CDare(this);
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        #endregion

        #region load

        protected override void LoadContent()
        {
            m_Dare.Config.General.WindowTitle = "Empty";
            m_Dare.Config.General.ShowMouseCursor = true;
            m_Dare.Config.General.ShowDebugConsole = true;
            m_Dare.Config.Video.ClearColor = Color.CornflowerBlue;


            m_Dare.Config.Video.FullScreen = false;
            m_Dare.Config.Video.SetResolution(1024, 768);
            m_Dare.Config.Video.MultiSampling = true;
            m_Dare.Config.Video.UnlockFps = true;

            m_Dare.RockNRoll();

            CreateRender();
            CreateScene();

        }

        #endregion

        #region creation

        private void CreateRender()
        {
            m_Scene = new DARE.CScene("scene");
            m_Render = new DARE.CRender3D("render");
            m_Scene.RegisterRenderer(m_Render);


            /*render.ShowModelNode = false;
            render.ShowCollision = true;
            render.Partitioner.Show = false;
            render.CollisionFillMode = FillMode.Solid;

            // light
            render.EffectMgr.LighteningEnabled = true;
            render.EffectMgr.AmbientLightColor = Color.White;
            render.EffectMgr.AmbientLightIntensity = 0.1f;

            render.EffectMgr.DirectionalLight0Direction = new Vector3(0.6f, -1, -0.8f);
            render.EffectMgr.DirectionalLight0DiffuseColor = Color.White;
            render.EffectMgr.DirectionalLight0DiffuseIntensity = 1.5f;*/


            tex = DARE.CResourceMgr.Load<Texture2D>("logo");
            DARE.CModel model = new DARE.CModel("cha_lez", "cha_lez");
            model.Effect.ColorMap = DARE.CResourceMgr.Load<Texture2D>("tex_lez");
            model.Effect.IsAnimationActive = true;

            DARE.CNode node = m_Render.CreateNode("cha_lez").SetPosition(0, 0, -5);
            node.Entity.AttachModel(model);
            //            node.Entity.Model.Effect.ColorMap = tex;

            m_Render.CreateNode().SetPosition(1, 1, -8).Entity.CreateBasicBox();

            m_Cam = new DARE.CFirstPersonCamera("fpc");
            m_Render.CameraMgr.RegisterCamera(m_Cam);
            m_Cam.LookAt(-Vector3.UnitZ);

#if WINDOWS_PHONE
            TouchPanel.EnabledGestures = GestureType.Tap;
#endif
        }
        Texture2D tex;

        private void CreateScene()
        {
        }

        #endregion

        #region run

        protected override void Update(GameTime gameTime)
        {
            ProcessKeyPressed();
            ProcessKeyReleased();
            ProcessMousePressed();
            ProcessMouseReleased();
            ProcessMouseMoved();
            ProcessMouseWheeled();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            m_Dare.SpriteBatch.Begin();
            m_Dare.SpriteBatch.Draw(tex, new Rectangle(0, 0, 200, 100), Color.White);
            m_Dare.SpriteBatch.End();
        }

        #endregion

        #region input

        private void ProcessKeyPressed()
        {
            foreach (Keys key in m_Dare.InputMgr.Keyboard.KeyPressedEvent)
                switch (key)
                {
                    case Keys.Escape: this.Exit(); break;
                    default: break;
                }

        }

        private void ProcessKeyReleased()
        {
        }

        private void ProcessMousePressed()
        {
#if WINDOWS_PHONE
            if (TouchPanel.IsGestureAvailable)
            {
                var gesture = TouchPanel.ReadGesture();
                if (gesture.GestureType == GestureType.Tap)
                {
                    CModel _model = (CModel)this.m_Render.GetNode("cha_lez").Entity.Model;
                    int i = 1;
                    var list = _model.GetAnimations();
                    if (list.Count > i)
                    {
                        CAnimConfig _config = _model.GetAnimConfig(list[i]);
                        if (_config == null)
                        {
                            _config = _model.Play(list[i], EAnimState.RUN | EAnimState.LOOP);
                            _config.Speed = 2.0f;

                        }
                        else if ((_config.State & EAnimState.PAUSE) == EAnimState.PAUSE)
                            _config.State = EAnimState.RUN;
                        else
                            _config.State = EAnimState.PAUSE;
                    }
                }
            }
#endif
        }

        private void ProcessMouseReleased()
        {
        }

        private void ProcessMouseMoved()
        {
        }

        private void ProcessMouseWheeled()
        {
        }

        #endregion
    }
}
